<?php
/**
 *      [Haidao] (C)2013-2099 Dmibox Science and technology co., LTD.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      http://www.haidao.la
 *      tel:400-600-2042
 */
core::load_class('init', 'admin');
class category_control extends init_control {
	public function _initialize() {
		parent::_initialize();
		$this->service = model('article_category','service');
	}
	/**
	 * [index 文章分类列表]
 	 */
	public function index(){
		$sqlmap = array();
		$sqlmap['parent_id'] = 0;
		$_GET['limit'] = isset($_GET['limit']) ? $_GET['limit'] : 10;
		$category = model('article_category')->where($sqlmap)->page($_GET['page'])->limit($_GET['limit'])->order("sort DESC")->select();
        $count = model('article_category')->where($sqlmap)->count();
        $pages = $this->admin_pages($count,$_GET['limit']);
		include $this->admin_tpl('category_index');
	}
	/**
	 * [add 添加分类]
	 */
	public function add(){
		if(checksubmit('dosubmit')){
			$result = $this->service->add($_GET);
			if(!$result){
				showmessage($this->service->error);
			}else{
				showmessage('操作成功','',1);
			}
		}else{
			if(!empty($_GET['id'])){
				$parent_name = $this->service->get_category_by_id($_GET['id'],'parent_name');
			}
			include $this->admin_tpl('category_edit');
		}
	}
	/**
	 * [edit_category 编辑分类]
 	 */
	public function edit(){
		if(checksubmit('dosubmit')){
			$result = $this->service->edit($_GET);
			if(!$result){
				showmessage($this->service->error);
			}else{
				showmessage('操作成功','',1);
			}
		}else{
			$info = $this->service->get_category_by_id($_GET['id']);
			include $this->admin_tpl('category_edit');
		}
	}
	/**
	 * [delete 删除分类]
 	 */
	public function delete(){
		$result = $this->service->delete($_GET);
		if(!$result){
			showmessage($this->service->error);
		}
		showmessage('操作成功',url('misc/category/index'),1);
	}
	/**
	 * [ajax_sun_class ajax获取分类]
	 */
	public function ajax_son_class(){
		$result = $this->service->ajax_son_class($_GET);
		if(!$result){
			showmessage($this->service->error,'',0,'','json');
		}else{
			showmessage('操作成功','',1,$result,'json');
		}
	}
	/**
	 * [ajax_edit 编辑分类名称]
 	 */
	public function ajax_edit(){
		$result = $this->service->ajax_edit($_GET);
		if(!$result){
			showmessage($this->service->error);
		}else{
			showmessage('操作成功','',1);
		}
	}
}